<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <AirportTree class="w-1/5 xl:w-1/5" v-show="!isCollapsed" @select="handleSelect"></AirportTree>
    <BasicTable :class="isCollapsed ? 'w-4/5 xl:w-5/5' : 'w-4/5 xl:w-4/5'" @register="registerTable" :rowSelection="rowSelection" :searchInfo="searchInfo">
      <template #toolbar>
        <CollapseContainer class="mt-4">
          <a-button type="primary" @click="toggleCollapse"> {{ isCollapsed ? '展开机场列表' : '折叠机场列表' }} </a-button>
          <a-button type="primary" @click="handleCreate" :disabled="!searchInfo.airportId"> 新增 </a-button>
          <a-button type="primary" @click="handleInitEmployeeCode"> 初始化邀请码</a-button>
        </CollapseContainer>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
    </BasicTable>
    <EmployeeDrawer @register="registerDrawer" @success="reload" />
    <EmployeeCopyDrawer @register="registerEmployeeCopyDrawer" @success="reload" />
    <AdjustDrawer @register="registerAdjustDrawer" @success="handleSuccess" />
    <EmployeeJobStateDrawer @register="registerEmployeeJobStateDrawer" @success="handleSuccess" />
    <EmployeeServerDrawer @register="registerEmployeeServerDrawer" />
    <EmployeeAdjustDrawer @register="registerEmployeeAdjustDrawer" />
    <EmployeeAirportDrawer @register="registerEmployeeAirportDrawer" />
    <EmployeeLogListDrawer @register="registerEmployeeLogListDrawer"></EmployeeLogListDrawer>
  </PageWrapper>
</template>

<script lang="ts" setup name="employee">
import { reactive, ref } from 'vue';
import { PageWrapper } from '@apps/antd-admin/src/components/page';
import AirportTree from '@apps/ant-bus/src/views/business/airport/components/AirportTree.vue';
import { ActionItem, BasicTable, TableAction } from '@apps/antd-admin/src/components/Table';
import { useDrawer } from '@apps/antd-admin/src/components/drawer';
import { CollapseContainer } from '@apps/antd-admin/src/components/Container/index';
import EmployeeDrawer from './components/EmployeeDrawer.vue';
import EmployeeCopyDrawer from './components/EmployeeCopyDrawer.vue';
import AdjustDrawer from './components/AdjustDrawer.vue';
import EmployeeJobStateDrawer from './components/EmployeeJobStateDrawer.vue';
import EmployeeServerDrawer from './components/EmployeeServerDrawer.vue';
import EmployeeAdjustDrawer from './components/EmployeeAdjustDrawer.vue';
import EmployeeAirportDrawer from './components/EmployeeAirportDrawer.vue';
import EmployeeLogListDrawer from './components/EmployeeLogListDrawer.vue';
import { useBaseTable } from '@apps/antd-admin/src/hooks';
import { columns, searchFormSchema } from './data/data';
import { Api, initEmployeeCode, resetPassWord } from './data/api';
import { useMessage } from '@apps/antd-admin/src/hooks/web/useMessage';

const isCollapsed = ref(false);
const { createMessage, createConfirm } = useMessage();
const [registerDrawer, { openDrawer }] = useDrawer();
const [registerEmployeeCopyDrawer, { openDrawer: openEmployeeCopyDrawer }] = useDrawer();
const [registerAdjustDrawer, { openDrawer: openAdjustDrawer }] = useDrawer();
const [registerEmployeeJobStateDrawer, { openDrawer: openEmployeeJobStateDrawer }] = useDrawer();
const [registerEmployeeAdjustDrawer, { openDrawer: openEmployeeAdjustDrawer }] = useDrawer();
const [registerEmployeeServerDrawer, { openDrawer: openEmployeeServerDrawer }] = useDrawer();
const [registerEmployeeAirportDrawer, { openDrawer: openEmployeeAirportDrawer }] = useDrawer();
const [registerEmployeeLogListDrawer, { openDrawer: openEmployeeLogListDrawer }] = useDrawer();
const { prefixCls, tableContext, handleDelete, handleSuccess, handlePost, handleBatchDelete } = useBaseTable({
  designScope: 'employee-template',
  tableProps: {
    title: '销售列表',
    rowKey: 'employeeId',
    //分页列表接口
    listUrl: Api.page,
    handlerName: 'busEmployeePageListHandler',
    //删除接口
    delUrl: Api.remove,
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    actionColumn: {
      width: 120,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
    clickToRowSelect: true,
    rowSelection: {
      type: 'radio',
    },
  },
});

const [registerTable, { reload }, { rowSelection, selectedRow, hasSelectedRows }] = tableContext;

/**
 * 行内操作栏
 */
function getTableAction(record): ActionItem[] {
  return [
    {
      label: '编辑',
      onClick: handleEdit.bind(null, record),
    },
  ];
}

/**
 * 下拉操作栏
 */
function getDropDownAction(record): ActionItem[] {
  return [
    {
      label: '重置密码',
      onClick: handleResetPassword.bind(null, record),
    },
    {
      label: '复制销售',
      onClick: handleCopy.bind(null, record),
    },
    {
      label: '删除',
      popConfirm: {
        title: '是否确认删除',
        confirm: handleDelete.bind(null, record),
      },
    },
    {
      label: '员工操作',
      onClick: handleEmployeeJobState.bind(null, record),
    },
    {
      label: '核销设置',
      onClick: handleEmployeeServer.bind(null, record),
    },
    {
      label: '调岗',
      onClick: handleAdjust.bind(null, record),
    },
    {
      label: '调岗记录',
      onClick: handleEmployeeRecord.bind(null, record),
    },
    {
      label: '查看日志',
      onClick: handlLookEmployeeLog.bind(null, record),
    },
    {
      label: '机场设置',
      onClick: handlEmployeeAirport.bind(null, record),
    },
  ];
}

const searchInfo = reactive<Recordable>({});

function toggleCollapse() {
  console.info(isCollapsed);
  isCollapsed.value = !isCollapsed.value;
}

function handleCreate() {
  let airportId = searchInfo.airportId;
  openDrawer(true, {
    isUpdate: false,
    airportId: airportId,
  });
}

function handleInitEmployeeCode() {
  createConfirm({
    iconType: 'info',
    title: '确认初始化销售邀请码',
    content: '确认初始化销售邀请码？',
    onOk: async () => {
      await initEmployeeCode();
      handleSuccess(true, {});
    },
    onCancel: async () => {},
  });
}

function handleCopy(record: Recordable) {
  openEmployeeCopyDrawer(true, {
    record,
    isUpdate: true,
  });
}

function handleEdit(record: Recordable) {
  openDrawer(true, {
    record,
    isUpdate: true,
  });
}

function handleEmployeeJobState(record: Recordable) {
  openEmployeeJobStateDrawer(true, {
    record,
    isUpdate: true,
  });
}

function handleEmployeeServer(record: Recordable) {
  openEmployeeServerDrawer(true, {
    record,
    isUpdate: false,
  });
}

function handleAdjust(record: Recordable) {
  openAdjustDrawer(true, {
    record,
    isUpdate: true,
  });
}

function handleEmployeeRecord(record: Recordable) {
  openEmployeeAdjustDrawer(true, {
    record,
    isUpdate: true,
  });
}

function handlLookEmployeeLog(record: Recordable) {
  openEmployeeLogListDrawer(true, {
    record,
    isUpdate: true,
  });
}

function handlEmployeeAirport(record: Recordable) {
  openEmployeeAirportDrawer(true, {
    record,
    isUpdate: true,
  });
}

function handleResetPassword(record: Recordable) {
  let employeeId = record.employeeId;
  createConfirm({
    iconType: 'info',
    title: '重置密码',
    content: '是否重置密码为qw12345678？',
    onOk: async () => {
      await resetPassWord({ employeeId: employeeId });
    },
    onCancel: async () => {},
  });
}

function handleSelect(airportId) {
  searchInfo.airportId = airportId;
  console.info('searchInfo', searchInfo);
  reload();
}
</script>
